home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 March: Reference Library / Dev.CD Mar 96 RL / Dev.CD Mar 96 RL.toast / Technical Documentation / develop / develop Issue 25 / develop Issue 25 code / Country Stringing (Newton) / Run Time Strings / WorldStrings.f < prev   
Encoding:
Text File  |  1995-12-15  |  5.3 KB  |  207 lines  |  [TEXT/R*ch]

  1. // Copyright ©1994-1995 Apple Computer, Inc.
  2. /*----------------------------------------------------------
  3.     Func: CreateLanguagesFrameFromText
  4.     
  5.     Purpose:
  6.         Create the languages frame from some text
  7.         files.
  8.         
  9.         Function first reads in the file defined by
  10.         GlobalsFilePath. The for each symbol in the
  11.         LanguagesSymArray, it will read in a file called
  12.         <sym>Strings.f, where sym in the symbol from the
  13.         array.
  14.         
  15.     Arguments:
  16.         GlobalsFilePath
  17.             full pathname to a file that will be Load'd before
  18.             the language files. Usually used to define
  19.             constants that can be used in the language files.
  20.             
  21.             Can be nil.
  22.             
  23.         LanguagesSymArray
  24.             Array of symbols for the languages to read in. The
  25.             symbol will be used as the slot name for the language.
  26.             
  27.             e.g. ['English, 'French, 'German]
  28.             
  29.             The first file read in will be "EnglishStrings.f", the slot
  30.             in the languages frame will be English.
  31.             
  32.     Returns:
  33.         The languages frame created
  34. ----------------------------------------------------------*/
  35.  
  36. DefGlobalFn('CreateLanguagesFrameFromText, func(GlobalsFilePath, LanguagesSymArray)
  37. begin
  38.     if GlobalsFilePath then
  39.         Load(GlobalsFilePath) ;
  40.     
  41.     local langFrame := {} ;
  42.     
  43.     foreach sym in LanguagesSymArray do
  44.         langFrame.(sym) := load(HOME & SprintObject(sym) & "Strings.f") ;
  45.     langFrame ;
  46. end) ;
  47.  
  48.  
  49.  
  50.  
  51. /*----------------------------------------------------------
  52.     Func: GetAllResources
  53.     
  54.     Purpose:
  55.         Create an array of binary objects that correspond
  56.         to all resources of a particular type that are
  57.         available.
  58.                 
  59.     Arguments:
  60.         ResType
  61.             string for the resource type to check for
  62.         
  63.         NewtType
  64.             type of object to create
  65.                     
  66.     Returns:
  67.         An array of the binary objects of resource types.
  68. ----------------------------------------------------------*/
  69.  
  70. DefGlobalFn('GetAllResources, func(ResType, NewtType)
  71. begin
  72.     local result := [];
  73.     local atID := 0 ;    
  74.  
  75.     // see if we can read in resource id 0, if so increment the
  76.     // next resource id, if not set the id to 128
  77.     try 
  78.         AddArraySlot(result, GetResource(ResType, atID, NewtType)) ;
  79.         atID := 1 ;
  80.         
  81.     onexception |evt.ex.msg| do
  82.         atID := 128 ;
  83.         
  84.     // start at the current resource id (either 1 or 128) and
  85.     // continue reading in resources until an exception occurs
  86.     loop
  87.     begin
  88.         try
  89.             AddArraySlot(result, GetResource(ResType, atID, NewtType)) ;
  90.             atID := atID + 1 ;
  91.         onexception |evt.ex.msg| do
  92.             break ;
  93.     end ;
  94.     result ;
  95. end);
  96.  
  97.  
  98. /*----------------------------------------------------------
  99.     Func: CreateLanguagesFrameFromRsrc
  100.     
  101.     Purpose:
  102.         Create the languages frame from a resource file
  103.         
  104.         Function looks for resources of type STR# by name.
  105.         There MUST be one called "Template" which is used
  106.         to construct a frame template.
  107.         
  108.         Then there can be any number of language specific
  109.         STR# resources.
  110.                 
  111.     Arguments:
  112.         ResFilePath
  113.             full pathname to the resource file containing the
  114.             required resources.
  115.             
  116.         LanguagesSymArray
  117.             Array of symbols for the languages to read in. The
  118.             symbol will be used as the slot name for the language.
  119.             
  120.             e.g. ['English, 'French, 'German]
  121.             
  122.             This will try to read STR# resources name "Template",
  123.             "English", "French" and "German".
  124.             
  125.     Returns:
  126.         The languages frame created
  127. ----------------------------------------------------------*/
  128.  
  129. DefGlobalFn('CreateLanguagesFrameFromRsrc, func(ResFilePath, LanguagesSymArray)
  130. begin
  131.     // Throw if there are not exactly five languages.
  132.     if Length(LanguagesSymArray) <> 5 then
  133.         Throw('|evt.ex.msg|, "The LanguagesSymArray must be exactly 5 elements long.");
  134.  
  135.     // the language frame array that will be returned
  136.     local langFrame := {} ;
  137.     foreach sym in LanguagesSymArray do
  138.         langFrame.(sym) := {} ;
  139.  
  140.     // could use a constant since currently must be exactly 5
  141.     // languages...
  142.     local numLanguages := Length(LanguagesSymArray) ;
  143.  
  144.     local r := OpenResFileX(ResFilePath) ;
  145.  
  146.     local locResourceArray := GetAllResources("LOC#", 'binaryObject);
  147.  
  148.     /* Process the LOC# resources.
  149.  
  150.         The format of the resource is:
  151.         16 bit count of number of strings ets
  152.         string set 1
  153.         string set 2...
  154.         string set n
  155.         
  156.         string set:
  157.             pathexpression as pascal string
  158.             English as C string
  159.             French as C string
  160.             German as C string
  161.             other1 as C string
  162.             other2 as C string
  163.     */
  164.     
  165.     local numStringSets ;
  166.     local pathExpr ;
  167.     local tempString ;
  168.     local atIndex ;
  169.  
  170.     foreach locResource in locResourceArray do
  171.     begin
  172.         // get the number of string sets
  173.         numStringSets := ExtractWord(locResource, 0) ;
  174.         
  175.         atIndex := 2 ;
  176.         //grab each string set
  177.         for stringSet := 1 to numStringSets do
  178.         begin
  179.             // grab the pstring that is the path
  180.             pathExpr := ExtractCString(locResource, atIndex) ;
  181.  
  182.             // update index counter
  183.             atIndex := atIndex + StrLen(pathExpr) + 1 ;
  184.  
  185.             // create path expression for following strings
  186.             pathExpr := call Compile("'" & pathExpr) with () ;
  187.  
  188.             // Get the language strings and jam them.
  189.             // WARNING: this code will ignore 0 length strings
  190.             // there are rare cases where you actually want
  191.             // an empty string for a particular translation. If
  192.             // this is the case, you could modify the code to
  193.             // throw an evt.ex.msg with the appropriate error.
  194.             foreach langSym in LanguagesSymArray do
  195.             begin
  196.                 tempString := ExtractCString(locResource, atIndex);
  197.                 if StrLen(tempString) > 0 then
  198.                     langFrame.(langSym).(pathExpr) := tempString ;
  199.                 atIndex := atIndex + StrLen(tempString) + 1
  200.             end;
  201.         end ;
  202.     end ;
  203.  
  204.     CloseResFileX(r) ;
  205.     langFrame;
  206. end);
  207.